from PyQt5.QtGui import QIcon, QPixmap, QPainter, QColor, QBrush, QPen, QFont
from PyQt5.QtCore import Qt, QRect, QSize


def create_document_icon(size=32, text=""):
    """创建一个文档图标"""
    pixmap = QPixmap(size, size)
    pixmap.fill(Qt.transparent)

    painter = QPainter(pixmap)
    painter.setRenderHint(QPainter.Antialiasing)

    # 绘制文档
    painter.setPen(QPen(QColor("#888888"), 1))
    painter.setBrush(QBrush(QColor("#f5f5f5")))
    painter.drawRect(size//4, size//8, size//2, 3*size//4)

    # 绘制文档折角
    painter.drawLine(3*size//4, size//8, 3*size//4, size//4)
    painter.drawLine(3*size//4, size//4, 3*size//4 - size//8, size//4)
    painter.drawLine(3*size//4 - size//8, size//4, 3*size//4, size//8)

    # 绘制文档内容线
    line_spacing = size // 10
    start_y = 3*size//8
    line_width = size//3

    for i in range(3):
        y = start_y + i * line_spacing
        painter.drawLine(size//3, y, size//3 + line_width, y)

    # 如果提供了文本，绘制文本
    if text:
        font = QFont("Arial", size//6)
        painter.setFont(font)
        painter.setPen(QColor("#555555"))
        painter.drawText(QRect(size//4, 5*size//8, size//2, size//4),
                         Qt.AlignCenter, text[:3])

    painter.end()
    return pixmap


def create_fallback_icon(extension="", size=32):
    """根据扩展名创建回退图标"""
    extension = extension.lower().replace(".", "")
    text = extension[:3].upper() if extension else "???"

    # 为不同类型的文件设置不同的颜色
    color = "#f5f5f5"  # 默认颜色

    # 常见文件类型的颜色映射
    ext_colors = {
        'exe': '#a5e8d5',  # 可执行文件 - 浅绿色
        'dll': '#d5e8a5',  # 库文件 - 浅黄色
        'txt': '#ffffff',  # 文本文件 - 白色
        'doc': '#a5c7e8',  # Word文档 - 浅蓝色
        'xls': '#a5e8b5',  # Excel文件 - 浅绿色
        'pdf': '#e8a5a5',  # PDF文件 - 浅红色
        'jpg': '#e8c5a5',  # 图像文件 - 浅橙色
        'png': '#e8c5a5',
        'mp3': '#d5a5e8',  # 音频文件 - 浅紫色
        'mp4': '#d5a5e8',  # 视频文件 - 浅紫色
    }

    if extension in ext_colors:
        color = ext_colors[extension]

    # 创建基础文档图标
    pixmap = QPixmap(size, size)
    pixmap.fill(Qt.transparent)

    painter = QPainter(pixmap)
    painter.setRenderHint(QPainter.Antialiasing)

    # 绘制文档
    painter.setPen(QPen(QColor("#888888"), 1))
    painter.setBrush(QBrush(QColor(color)))
    painter.drawRect(size//4, size//8, size//2, 3*size//4)

    # 绘制文档折角
    painter.drawLine(3*size//4, size//8, 3*size//4, size//4)
    painter.drawLine(3*size//4, size//4, 3*size//4 - size//8, size//4)
    painter.drawLine(3*size//4 - size//8, size//4, 3*size//4, size//8)

    # 如果提供了扩展名，绘制文本
    if extension:
        font = QFont("Arial", size//6, QFont.Bold)
        painter.setFont(font)
        painter.setPen(QColor("#555555"))
        painter.drawText(QRect(size//4, 5*size//8, size//2, size//4),
                         Qt.AlignCenter, text)

    painter.end()
    return pixmap


def create_app_icon_fallback(size=32):
    """创建应用图标的回退版本"""
    pixmap = QPixmap(size, size)
    pixmap.fill(Qt.transparent)

    painter = QPainter(pixmap)
    painter.setRenderHint(QPainter.Antialiasing)

    # 画圆形背景
    painter.setPen(Qt.NoPen)
    painter.setBrush(QBrush(QColor("#0078d4")))
    painter.drawEllipse(2, 2, size-4, size-4)

    # 画闪电符号
    painter.setPen(QPen(QColor("white"), size//16))

    # 绘制闪电
    mid_x = size // 2
    top_y = size // 4
    bottom_y = 3 * size // 4

    points = [
        (mid_x, top_y),
        (mid_x - size//8, mid_y),
        (mid_x + size//8, mid_y),
        (mid_x, bottom_y)
    ]

    painter.drawLine(points[0][0], points[0][1], points[1][0], points[1][1])
    painter.drawLine(points[1][0], points[1][1], points[2][0], points[2][1])
    painter.drawLine(points[2][0], points[2][1], points[3][0], points[3][1])

    painter.end()
    return QIcon(pixmap)


if __name__ == "__main__":
    # 测试代码
    app_icon = create_app_icon_fallback(64)
    doc_icon = create_document_icon(64)
    txt_icon = create_fallback_icon("txt", 64)

    # 保存测试图标
    app_icon.pixmap(64, 64).save("app_fallback.png")
    QPixmap.fromImage(doc_icon.toImage()).save("doc_fallback.png")
    QPixmap.fromImage(txt_icon.toImage()).save("txt_fallback.png")

    print("已创建测试图标")
